﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.Charence.PassManager.BusinessEntities.Dashboard;
using Com.Charence.PassManager.Common;
using Com.Charence.PassManager.DataComponents;

namespace Com.Charence.PassManager.BusinessComponents
{
    public class DashboardBC
    {
        private readonly DashboardDC _dc = new DashboardDC();

        public List<AccountSummary> GetLast10AccountSummary(int userId)
        {
            var summaryList = _dc.GetLast10AccountSummary(userId);

            var result = new List<AccountSummary>();

            summaryList.ForEach(p => 
            { 
                if(result.Any(r => r.AccountID == p.AccountID)) return;

                var summary = new AccountSummary();

                summary.AccountID = p.AccountID;
                summary.AccountName = p.Account;
                summary.WebSiteName = p.WebSiteName;

                summary.IsPinSet = summaryList.Any(s => s.AccountID == p.AccountID && s.PropertyTypeID == Constants.PropertyType.PIN);
                summary.OtherProperties = summaryList.Count(s => s.AccountID == p.AccountID && s.PropertyTypeID == Constants.PropertyType.Custom);

                var secureQuestionCount = 0;

                secureQuestionCount += Convert.ToInt32(
                    summaryList.Any(s => s.AccountID == p.AccountID 
                        && s.PropertyTypeID == Constants.PropertyType.SecureQuestion1));
                secureQuestionCount += Convert.ToInt32(
                    summaryList.Any(s => s.AccountID == p.AccountID 
                        && s.PropertyTypeID == Constants.PropertyType.SecureQuestion2));
                secureQuestionCount += Convert.ToInt32(
                    summaryList.Any(s => s.AccountID == p.AccountID 
                        && s.PropertyTypeID == Constants.PropertyType.SecureQuestion3));

                if (secureQuestionCount > 0)
                {
                    summary.SecureQuestions = secureQuestionCount;
                }

                result.Add(summary);
            });

            return result;
        }
    }
}
